package com.bw.unit3.view.activity

import androidx.navigation.fragment.NavHostFragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.unit3.R
import com.bw.unit3.base.BaseDataBindingActivity
import com.bw.unit3.databinding.ActivityMainBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener

@Route(path = "/main/MainActivity")
class MainActivity : BaseDataBindingActivity<ActivityMainBinding>(R.layout.activity_main) {
    lateinit var navHostFragment: NavHostFragment

    override fun initView() {
        //获取宿主navHostFragment
        navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragment_id) as NavHostFragment

        //获取导航控制器
        val navController = navHostFragment.navController

        dataBinding.tablayout.addTab(dataBinding.tablayout.newTab().setText("首页").setIcon(R.drawable.ic_phone_24))
        dataBinding.tablayout.addTab(dataBinding.tablayout.newTab().setText("分类").setIcon(R.drawable.ic_phone_24))
        dataBinding.tablayout.addTab(dataBinding.tablayout.newTab().setText("购物车").setIcon(R.drawable.ic_phone_24))
        dataBinding.tablayout.addTab(dataBinding.tablayout.newTab().setText("我的").setIcon(R.drawable.ic_phone_24))
        dataBinding.tablayout.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when (tab?.position) {
                    0 -> {
                        navController.navigate(R.id.homeFragment)
                    }

                    1 -> {
                        navController.navigate(R.id.typeFragment)
                    }

                    2 -> {
                        navController.navigate(R.id.cartFragment)
                    }

                    3 -> {
                        navController.navigate(R.id.mineFragment)
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })

    }

}